import 'package:check_app/dao/userInfo_dao.dart';
import 'package:check_app/util/uploadImage_util.dart';
import 'package:check_app/widgets/Picture.dart';
import 'package:check_app/widgets/deleteModal.dart';
import 'package:check_app/widgets/downModal.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';

class MyCredentialsPage extends StatefulWidget {
  final List<String> certificateUrlList;
  const MyCredentialsPage({Key key, this.certificateUrlList}) : super(key: key);

  @override
  State<StatefulWidget> createState() => new _MyCredentialsPageState();
}

class _MyCredentialsPageState extends State<MyCredentialsPage> {
  bool _loading = false;
  List<String> picList;
  bool editType;

  @override
  void initState() {
    super.initState();
    editType = false;
    picList = widget.certificateUrlList ?? [];
  }

  Future<Null> saveCert(List urlList) async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await UserInfoDao.saveCert({'certificateUrlList': urlList});
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          picList = urlList;
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(248, 248, 249, 1),
      appBar: AppBar(
        centerTitle: true,
        title: Text('资质证书管理'),
        actions: <Widget>[
          // GestureDetector(
          //   onTap: () {
          //     setState(() {
          //       editType = !editType;
          //     });
          //   },
          //   child: Container(
          //     alignment: Alignment.center,
          //     padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
          //     child: Text(editType ? '取消' : '编辑'),
          //   ),
          // ),
        ],
      ),
      body: SafeArea(
        child: ModalProgressHUD(
          inAsyncCall: _loading,
          child: Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.all(16),
                    child: Picture(
                      count: 3,
                      picList: picList,
                      onAdd: () {
                        showModalBottomSheet(
                          context: context,
                          builder: (BuildContext context) {
                            return DownModal(
                              cameraCb: () async {
                                final url = await UploadImageUtil()
                                    .getImageFromCamera();
                                List<String> list = picList.sublist(0);
                                list.add(url);
                                saveCert(list);
                              },
                              photoCb: () async {
                                final urlList = await UploadImageUtil()
                                    .loadAssets(9 - picList.length);
                                List<String> list = picList.sublist(0);
                                for (var value in urlList) {
                                  list.add(value.toString());
                                }
                                saveCert(list);
                              },
                            );
                          },
                        );
                      },
                      onRemove: (url) {
                        showModalBottomSheet(
                          context: context,
                          builder: (BuildContext context) {
                            return DeleteModal(
                              onRemove: () {
                                List<String> list = picList.sublist(0);
                                list.remove(url);
                                saveCert(list);
                              },
                            );
                          },
                        );
                      },
                    ),
                  ),
                ],
              ),
              editType
                  ? Positioned(
                      left: 0,
                      right: 0,
                      bottom: 0,
                      child: Container(
                        height: 49,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(255, 255, 255, 1),
                          border: Border(
                            top: BorderSide(
                              width: 0.5,
                              color: Color.fromRGBO(229, 229, 229, 1),
                            ),
                          ),
                        ),
                        child: Text(
                          '删除',
                          style: TextStyle(
                            fontSize: 16,
                            height: 22 / 16,
                            color: Color.fromRGBO(240, 64, 64, 1),
                          ),
                        ),
                      ),
                    )
                  : SizedBox(),
            ],
          ),
        ),
      ),
    );
  }
}
